﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HCenter.CacheHelper
{
    public static class CacheFactory
    {
        private static ICacheProvider cache = null;
        private static readonly object lockHelper = new object();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cacheTargetType"></param>
        /// <param name="dbname"></param>
        /// <param name="dbIndex"></param>
        /// <param name="KeySuffix"></param>
        /// <returns></returns>
        public static ICacheProvider getCacheProvider(CacheTargetType cacheTargetType, string dbname, int dbIndex, string KeySuffix = "")
        {
            if (cache == null)
            {
                lock (lockHelper)
                {
                    if (cache == null)
                    {
                        switch (cacheTargetType)
                        {
                            case CacheTargetType.Redis: cache = new CSRedisCacheImp(dbname, dbIndex) { KeySuffix = KeySuffix }; break;
                            default:
                            case CacheTargetType.MemoryCache: cache = new MemoryCacheImp(dbname) { KeySuffix = "dbIndex" + dbIndex + KeySuffix }; break;
                        }
                    }
                }
            }
            return cache;
        }
    }
}